<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link href="../../../jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>

    <script type="text/javascript" src="../../../jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../../../jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div>
    <div style="position: relative; left: 30px; top: -10px;">
        <div class="page-header">
            <h3>字典类型列表</h3>
        </div>
    </div>
</div>
<div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;left: 30px;">
    <div class="btn-group" style="position: relative; top: 18%;">
        <button type="button" class="btn btn-primary" onclick="window.location.href='save.html'"><span
                class="glyphicon glyphicon-plus"></span> 创建
        </button>
        <button type="button"  id="btnEdit" class="btn btn-default" ><span
                class="glyphicon glyphicon-edit"></span> 编辑
        </button>
        <button type="button"  id="btnDel" class="btn btn-danger"><span class="glyphicon glyphicon-minus"></span> 删除</button>
    </div>
</div>
<div style="position: relative; left: 30px; top: 20px;">
    <table id="table" class="table table-hover">
        <thead>
        <tr style="color: #B3B3B3;">
            <td><input id="selectAll" type="checkbox"/></td>
            <td>序号</td>
            <td>编码</td>
            <td>名称</td>
            <td>描述</td>
        </tr>
        </thead>
        <tbody>
        <tr class="active">
            <td><input type="checkbox"/></td>
            <td>1</td>
            <td>sex</td>
            <td>性别</td>
            <td>性别包括男和女</td>
        </tr>
        </tbody>
    </table>
</div>

</body>
</html>

<script>
    function getAll() {
        $.ajax({
            url: "/DictionaryType/getAll.action",
            dataType:"json",
            //data:"",
            beforeSend: function (XMLHttpRequest) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data, textStatus, jqXHR) {
				$("#table tbody").empty();
				$(data).each(function (i,e) {
					var tamp=' <tr >\n' +
							'            <td><input name="code" type="checkbox" value="'+e.code+'"  /></td>\n' +
							'            <td>'+(i+1)+'</td>\n' +
							'            <td>'+e.code+'</td>\n' +
							'            <td>'+e.name+'</td>\n' +
							'            <td>'+e.description+'</td>\n' +
							'        </tr>';
					$("#table tbody").append(tamp);
				});
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    }


    $(function () {

        //checkBox全选
        $("#selectAll").click(function (){
            $(":checkbox[name=code]").prop("checked",$("#selectAll").prop("checked"));
        });



///修改按钮

        $("#btnEdit").click(function () {
            //jQuery数组对象
            var $delCode=$(":checkbox[name=code]:checked");

            if($delCode.size()==0){
                window.alert("你没有选任何类型，无法修改");
                return;
            }

            var  code = $($delCode[0]).val();
            window.location="edit.html?id="+code;

            //window.location="edit.html";

        });

        //删除按钮

        $("#btnDel").click(function (){

            //jQuery数组对象
            var $delCode=$(":checkbox[name=code]:checked");

            if($delCode.size()==0){
                window.alert("你没有选任何类型，无法删除");
                return;
            }

            if(!window.confirm("确定删除吗？")){
                return;
            }
            //确定要删除了
            var codes=[];
            //循环所有的CheckBox对象
            $delCode.each(function (i,e) {
                //获取每个CheckBox对象的value值，并且添加到codes数组
                codes.push(e.value);
            });
            //把数组变成字符串自己传递给后端，避免前端告诉知道传递的是JSON格式
            //var ids = codes.join(",");
            //前端传递的JSON数据不管是原生态还是jQuery对象，都需要使用JSON.stringify格式化
            var codes = JSON.stringify(codes);

            //ajax请求
            	$.ajax({
            			url:"/DictionaryType/del.action",
                        type:"post",
            			dataType:"json",
                        contentType:"application/json",
            			data:codes,
            			beforeSend:function (XMLHttpRequest) {

            			},
            			error:function (XMLHttpRequest,textStatus, errorThrown) {

            			},
            			success:function (data, textStatus, jqXHR) {
                            if(data['result']=="success"){
                                getAll();
                            }
                            else{
                                window.alert(data['mess']);
                            }
            			},
            			complete:function (XMLHttpRequest, textStatus) {
            			}
            		});


        });


        //全查
        getAll();

    });

</script>